<?php
/*
 * ebiz front
 *
 * CategoryController
 *
 * @author allen
 *
 */

require_once '../app/core/controllers/BaseController.php';
require_once '../app/services/CategoryService.php';
require_once '../app/core/utils/HttpUtil.class.php';
require_once '../app/views/BaseView.php';
require_once '../app/views/CategoryView.php';
require_once "../". $GLOBALS["local"] ."/"."category.php";
require_once "../". $GLOBALS["local"] ."/"."myaccount.php";

class CategoryController extends BaseController {

	public function index($param = array()){

		$config = $GLOBALS["config"];
        $urlTitle = urldecode($GLOBALS['config']['urlparam'][3]);

        $arr_param = array();

		$category = new CategoryService();
		$arr_selectCategoryData = $category->getByUrlTitle($urlTitle);
		if( empty($arr_selectCategoryData[0] ) ){
            		HttpUtil::renderError();
		}
		$arr_selectCategoryData = $arr_selectCategoryData[0];
		if(empty($arr_selectCategoryData)){
			HttpUtil::renderError();
		}

		$arr_param['category'] = $arr_selectCategoryData;
        $categoryView = new CategoryFirstView();
        $categoryView->addCSS('productlist.css');
        $categoryView->addJs('class1.js');
        $str_content = $categoryView->getCategoryFirstViewPage($arr_param);
        echo $str_content;

        exit();
	}

	public function search($arr_param = array(), $bool_ajax = false){
	    /**
	     * 搜索条件处理
	     */
	    if ( empty( $arr_param[0] ) ){
	        HttpUtil::renderError( 'Parameter Error' . __FILE__ . ':' . __LINE__ );
	    }
	    $int_categoryid = array_shift($arr_param);
        $str_otherFilter = " AND `category_id` = '{$int_categoryid}'";
	    /**
	     * 参数形式为metadata_id,metadata_option_id,metadata_id,metadata_option_id...
	     */
	    if (!empty($arr_param)) {
	        $int_paramlength = count($arr_param);
    	    $arr_search = array();
    	    $arr_sortby = array(
    	       'id' => 1, //default
    	       'price' => 0, //-1,0,1
    	       'created' => 0 //-1,0,1
    	    );
    	    $int_nowpage = 1;
	        for ($int_i=0; $int_i<$int_paramlength; $int_i+=2){
	            if (!empty($arr_param[$int_i]) && !empty($arr_param[$int_i+1])) {
	               if (strtolower($arr_param[$int_i])=='price') {
	                   $arr_sortby['price'] = $arr_param[$int_i+1];
	                   $arr_sortby['id'] = $arr_sortby['created'] = 0;
	               } elseif (strtolower($arr_param[$int_i])=='created') {
	                   $arr_sortby['created'] = $arr_param[$int_i+1];
	                   $arr_sortby['id'] = $arr_sortby['price'] = 0;
	               } elseif (strtolower($arr_param[$int_i])=='page') {
	                   $int_nowpage = $arr_param[$int_i+1];
	               } elseif (strtolower($arr_param[$int_i])=='money') {
	                   $arr_money = explode('-', $arr_param[$int_i+1]);
	                   $str_otherFilter .= " AND `pi`.`sale_price` >= '{$arr_money[0]}' ";
	                   if (isset($arr_money[1]))
	                        $str_otherFilter .= " AND `pi`.`sale_price` <= '{$arr_money[1]}' ";
	               } else {
	                   $arr_search['auto_metadata_' . $arr_param[$int_i]] = $arr_param[$int_i+1];
	               }
	            }
	        }
	    }

	    /**
	     * @desc 分页设置
	     */
	    $int_countrows = 0;
	    $categoryService = new CategoryService();
	    $arr_navigation = $categoryService->getParentCategory($int_categoryid);
	    $arr_metadatas = $categoryService->getMeatadatasByCategoryId($int_categoryid);
	    $arr_products = $categoryService->getProductItemsByCategoryId($int_nowpage, $int_countrows, $int_categoryid, $arr_search, $arr_sortby, $str_otherFilter);
	    $arr_pager = array(
	       'nowpage' => $int_nowpage,
	       'countrows' => $int_countrows
	    );

	    require_once '../app/views/CategorySearchView.php';
	    $categorySearchView = new CategorySearchView();
	    $categorySearchView->category_id = $int_categoryid;
	    $categorySearchView->metadatas = $arr_metadatas;
	    $categorySearchView->products = $arr_products;
	    $categorySearchView->pager = $arr_pager;
	    $categorySearchView->sort = $arr_sortby;
	    $categorySearchView->filter = $arr_search;
	    $categorySearchView->navigation = $arr_navigation;
	    if( !$bool_ajax )
	       echo $categorySearchView->getPage();
	    else
	       return $categorySearchView->getSearchResultBlock();
	    exit;
	}

}


